package codepulse.blogmoon.cn.system.controller;

import codepulse.blogmoon.cn.common.context.Context;
import codepulse.blogmoon.cn.common.entity.Result;
import codepulse.blogmoon.cn.common.exception.BizException;
import codepulse.blogmoon.cn.system.dto.admin.AdminAcctInfoDTO;
import codepulse.blogmoon.cn.system.dto.SysAcctInfoDTO;
import codepulse.blogmoon.cn.system.dto.admin.RegisterDTO;
import codepulse.blogmoon.cn.system.dubbo.AcctInfoDubboService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;


@RestController
@RequestMapping("/acct")
public class AcctInfoController {

    @Resource
    private AcctInfoDubboService acctInfoDubboService;


    @GetMapping("/admin/info")
    public Result<AdminAcctInfoDTO> getAdminAcctInfo() {
        return Result.success(acctInfoDubboService.getAdminAcctInfo(Context.get().getBizAcctId()));
    }

    @GetMapping("/info")
    public Result<SysAcctInfoDTO> getAcctInfo() {
        SysAcctInfoDTO acctInfo = acctInfoDubboService.getAcctInfo(Context.get().getBizAcctId());
        if (Objects.isNull(acctInfo)){
            throw new BizException("不存在当前账号信息，请注册后重新登录");
        }
        return Result.success(acctInfo);
    }

    @PostMapping("/register")
    public Result register(@RequestBody RegisterDTO dto) {
        acctInfoDubboService.register(dto);
        return Result.success();
    }
}
